﻿@{
    ViewBag.Title = "Index";
}

<h1>Привет!</h1>
<button type="button" class="btn btn-primary btn-mega-large" data-target="#registration-modal" data-toggle="modal">Регистрация</button>
<button type="button" class="btn btn-inverse btn-mega-large" data-target="#login-modal" data-toggle="modal">Вход</button>

<div class="modal hide fade" id="registration-modal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3>Это быстро</h3>
    </div>  
    <div class="modal-body">
        <p>Заполни всего 3 поля. Никаких подтверждений не потребуется</p>
        @using (Html.BeginForm("register", "account", FormMethod.Post, new { @class = "form-horizontal", id = "registration-form" }))
        {
            <div class="control-group">
                <label class="control-label" for="register-login">Псевдоним</label>
                <div class="controls">
                    <input type="text" name="login" id="register-login" placeholder="@@псевдоним">
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="register-email">Электронная почта</label>
                <div class="controls">
                    <input type="text" name="email" id="register-email" placeholder="name@@mailbox.com">
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="register-password">Пароль</label>
                <div class="controls">
                    <input type="password" name="password" id="register-password" placeholder="пароль">
                </div>
            </div>
        }
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Отмена</button>
        <button class="btn btn-primary" type="submit">Регистрация</button>
    </div>
</div>
<div class="modal hide fade" id="login-modal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3>Заходи</h3>
    </div>
    <div class="modal-body">
        <p>Укажи свои данные</p>
        @using (Html.BeginForm("login", "account", FormMethod.Post, new {@class = "form-horizontal", id="login-form"}))
        {
            <div class="control-group">
                <label class="control-label" for="login-login">Псевдоним</label>
                <div class="controls">
                    <input type="text" name="login" id="login-login" placeholder="@@псевдоним">
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="login-password">Пароль</label>
                <div class="controls">
                    <input type="password" name="password" id="login-password" placeholder="пароль">
                </div>
            </div>
        }
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Отмена</button>
        <button class="btn btn-primary" type="submit">Вход</button>
    </div>
</div>

<script type="text/javascript">
    $('#registration-modal > .modal-footer > button.btn-primary').click(function () {
        var login = $('#register-login').val();
        var email = $('#register-email').val();
        var password = $('#register-password').val();

        if (login == '' || email == '' || password == '') {
            alert('one of is empty!');
            return;
        }

        var params =
            {
                url: '@Url.Action("register", "account")',
                type: 'POST',
                dataType: 'html',
                data: {
                    login: login,
                    email: email,
                    password: password
                },
                complete: function (j, status) {
                    alert($.parseJSON(j.responseText).message);
                }
            };

        $.ajax(params);
    });

    $('#login-modal > .modal-footer > button.btn-primary').click(function () {
        var login = $('#login-login').val();
        var password = $('#login-password').val();

        if (login == '' || password == '') {
            alert('one of is empty!');
            return;
        }

        var params =
            {
                url: '@Url.Action("login", "account")',
                type: 'POST',
                dataType: 'html',
                data: {
                    login: login,
                    password: password
                },
                complete: function (j, status) {
                    var result = $.parseJSON(j.responseText);
                    if (result.success == true)
                        window.location = result.returnUrl;
                    else
                        alert(result.message);
                }
            };

        $.ajax(params);
    });
</script>